package com.packtpublishing.tddjava.ch04ship;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

import static org.testng.Assert.assertEquals;

@Test
public class ShipSpec {

    private Ship ship;
    private Location location;
    private Planet planet;
    private List<Point> obstacles;

    @BeforeMethod
    public void beforeMethod() {
        location = new Location(new Point(21, 13), Direction.EAST);

        Point max = new Point(50, 50);

        obstacles = new ArrayList<Point>();
        obstacles.add(new Point(12, 13));
        obstacles.add(new Point(16, 32));
        planet = new Planet(max, obstacles);

        ship = new Ship(location, planet);
    }

    public void whenInitThenLocationIsSet() {
        assertEquals(ship.getLocation(), location);
    }

    public void whenInitThenPlanetIsStored() {
        assertEquals(ship.getPlanet(), planet);
    }
    public void whenInitThenPlanetObstaclesAreStored() {
        assertEquals(ship.getPlanet().getObstacles(), obstacles);
    }

    //意图：应在 Ship 类中创建方法 moveForward ，并确保这个方法调用了 location.forward
    public void whenMoveForwardThenForward() {
        Location expected = location.copy();
        expected.forward();
        ship.moveForward();
        assertEquals(ship.getLocation(), expected);
    }

    public void whenMoveBackwardThenBackward() {
        Location expected = location.copy();
        expected.backward();
        ship.moveBackward();
        assertEquals(ship.getLocation(), expected);
    }

    public void whenTurnLeftThenLeft() {
        Location expected = location.copy();
        expected.turnLeft();
        ship.turnLeft();
        assertEquals(ship.getLocation(), expected);
    }

    public void whenTurnRightThenRight() {
        Location expected = location.copy();
        expected.turnRight();
        ship.turnRight();
        assertEquals(ship.getLocation(), expected);
    }

    public void whenReceiveCommandFThenForward() {
        Location expected = location.copy();
        expected.forward();
        ship.receiveCommands("f");
        assertEquals(ship.getLocation(), expected);
    }

    public void whenReceiveCommandsThenAllAreExcuted() {
        Location expected = location.copy();
        expected.forward();
        expected.turnRight();
        expected.backward();
        expected.turnLeft();

        ship.receiveCommands("frbl");
        assertEquals(ship.getLocation(), expected);
    }

    public void overpassEastBoundary() {
        location.setDirection(Direction.EAST);
        location.getPoint().setX(planet.getMax().getX());
        ship.receiveCommands("f");
        assertEquals(location.getX(), 1);
    }

    /*
    每次移动前都进行障碍检测。如果执行指定的命令将遇到障碍，军舰应放弃
移动，留在原地并报告遇到的障碍
     */
    public void whenObstacleThenStopAndReport(){
        location.setDirection(Direction.EAST);
        location.getPoint().setX(11);
        location.getPoint().setY(13);
        String result = ship.receiveCommands("f");
        assertEquals(result, "X");
        assertEquals(location.getX(), 11);

    }

}
